home *** CD-ROM | disk | FTP | other *** search
/ T&A 2 the Maxx 3 / T and A 2 The Maxx Number 3.iso / viewers / unixview / xgiftar.z / xgiftar / options.c < prev    next >
C/C++ Source or Header  |  1991-05-20  |  11KB  |  291 lines

  1. /* options.c:
  2.  *
  3.  * finds which option in an array an argument matches
  4.  *
  5.  * jim frost 10.03.89
  6.  *
  7.  * Copyright 1989 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include <stdio.h>
  13. #include "options.h"
  14.  
  15. /* options array and definitions.  If you add something to this you also
  16.  * need to add its OptionId in options.h.
  17.  */
  18.  
  19. static OptionArray Options[] = {
  20.  
  21.   /* global options
  22.    */
  23.  
  24.   { "border",     BORDER,     "color", "\
  25. Set the color used for the border around centered or placed images.", },
  26.   { "debug",      DBUG,       NULL, "\
  27. Turn on synchronous mode for debugging.", },
  28.   { "default",    DEFAULT,    NULL, "\
  29. Set the root background to the default pattern and colors.", },
  30.   { "delay",      DELAY,      "seconds", "\
  31. Set the automatic advance delay for all images.", },
  32.   { "display",    DISPLAY,    NULL, "\
  33. Indicate the X display you would like to use.", },
  34.   { "fit",        FIT,        NULL, "\
  35. Force the image(s) to use the default colormap.", },
  36.   { "fork",       FORK,       NULL, "\
  37. Background automatically.  Turns on -quiet.", },
  38.   { "fullscreen", FULLSCREEN, NULL, "\
  39. Use the whole screen for displaying an image.  If -onroot is also specified,\n\
  40. the image will be zoomed to fit the size of the screen.", },
  41.   { "geometry",   GEOMETRY,   "window_geometry", "\
  42. Specify the size of the display window.  Ignored if -fullscreen is given.\n\
  43. If used in conjunction with -onroot, this defines the size of the base image.", },
  44.   { "help",       HELP,       "[option ...]", "\
  45. Give help on a particular option or series of options.  If no option is\n\
  46. supplied, a list of available options is given.", },
  47.   { "identify",   IDENTIFY,   NULL, "\
  48. Identify images rather than displaying them.", },
  49.   { "install",    INSTALL,    NULL, "\
  50. Force colormap installation.  This option is useful for naive window managers\n\
  51. which do not know how to handle colormap installation, but should be avoided\n\
  52. unless necessary.", },
  53.   { "list",       LIST,       NULL, "\
  54. List the images along the image path.  Use `xloadimage -path' to see the\n\
  55. current image path.", },
  56.   { "onroot",     ONROOT,     NULL, "\
  57. Place the image on the root window.  If used in conjunction with -fullscreen,\n\
  58. the image will be zoomed to fit.  -border, -at, and -center also affect the\n\
  59. results.", },
  60.   { "path",       PATH,       NULL, "\
  61. Display the image path and default extensions that are loaded from the\n\
  62. .xloadimagerc file.", },
  63.   { "pixmap",     PIXMAP,     NULL, "\
  64. Force the use of a pixmap as backing store.  This may improve performance but\n\
  65. may not work on memory-limited servers.", },
  66.   { "private",    PRIVATE,    NULL, "\
  67. Force the use of a private colormap.  This happens automatically if a visual\n\
  68. other than the default is used.  This is the opposite of -fit.", },
  69.   { "quiet",      QUIET,      NULL, "\
  70. Turn off verbose mode.  This is the default if using -onroot or -windowid.", },
  71.   { "supported",  SUPPORTED,  NULL, "\
  72. Give a list of the supported image types.", },
  73.   { "verbose",    VERBOSE,    NULL, "\
  74. Turn on verbose mode.  This is the default if using -view.", },
  75.   { "version",    VER_NUM,    NULL, "\
  76. Show the version number of this version of xloadimage.", },
  77.   { "view",       VIEW,       NULL, "\
  78. View an image in a window.  This is the default for all but xsetbg.", },
  79.   { "visual",     VISUAL,     NULL, "\
  80. Force the use of a particular visual to display an image.  Normally xloadimage\n\
  81. will attempt to pick a visual which is reasonable for the supplied image.", },
  82.   { "windowid",   WINDOWID,   "window_id", "\
  83. Set the background of a particular window.  This is similar to -onroot and\n\
  84. is useful for servers which use an untagged virtual root.  The window ID\n\
  85. should be supplied as a hexadecimal number, eg 0x40003.", },
  86.  
  87.   /* image options
  88.    */
  89.  
  90.   { "at",         AT,         NULL, "\
  91. Load the image onto the base image (if using -merge) or the root window (if\n\
  92. using -onroot) at a specific location.  If used in conjunction with -onroot\n\
  93. this forces the -fullscreen flag on.", },
  94.   { "background", BACKGROUND, "color", "\
  95. Set the background pixel color for a monochrome image.  See -foreground and\n\
  96. -invert.", },
  97.   { "brighten",   BRIGHT,     "percentage", "\
  98. Brighten or darken the image by a percentage.  Values greater than 100 will\n\
  99. brighten the image, values smaller than 100 will darken it.", },
  100.   { "center",     CENTER,     NULL, "\
  101. Center the image on the base image (if using -merge) or the root window (if\n\
  102. using -onroot).  If used in conjunction with -onroot this forces the\n\
  103. -fullscreen flag on.", },
  104.   { "clip",       CLIP,       "X,Y,W,H", "\
  105. Clip out the rectangle specified by X,Y,W,H and use that as the image.", },
  106.   { "colors",     COLORS,     "number_of_colors", "\
  107. Specify the maximum number of colors to be used in displaying the image.\n\
  108. Values of 1-32768 are acceptable although low values will not look good.\n\
  109. This is done automatically if the server cannot support the depth of the\n\
  110. image.", },
  111.   { "dither",     DITHER,     NULL, "\
  112. Dither the image into monochrome.  This happens automatically if sent to\n\
  113. a monochrome display.", },
  114.   { "foreground", FOREGROUND, "color", "\
  115. Set the foreground pixel color for a monochrome image.  See -background and\n\
  116. -invert.", },
  117.   { "gamma",      GAMMA,      "value", "\
  118. Adjust the image's colors by a gamma value.  1.0 is the default, which does\n\
  119. nothing.  Values under 1.0 darken the image, values higher brighten it.\n\
  120. Typically a dark image needs a value of 2.0 to 2.5.  This option is similar\n\
  121. to -brighten but often works better.", },
  122.   { "goto",       GOTO,       "image_name", "\
  123. Start displaying a particlar image.  This is useful for creating image display\n\
  124. loops.  If two images with the target name exist, the first in the argument list\n\
  125. will be used.", },
  126.   { "gray",       GRAY,       NULL, "\
  127. Convert a color image to grayscale.  Also called -grey.", },
  128.   { "grey",       GRAY,       NULL, "\
  129. See -gray.", },
  130.   { "halftone",   HALFTONE,   NULL, "\
  131. Dither the image into monochrome using a halftone dither.  This preserves\n\
  132. image detail but blows the image up by sixteen times.", },
  133.   { "idelay",     IDELAY,     NULL, "\
  134. Set the automatic advance delay for this image.  This overrides -delay\n\
  135. temporarily.", },
  136.   { "invert",     INVERT,     NULL, "\
  137. Invert a monochrome image.  This is the same as specifying `-foreground black'\n\
  138. and `-background white'.", },
  139.   { "merge",      MERGE,      NULL, "\
  140. Merge this image onto the previous image.  When used in conjunction with\n\
  141. -at, -center, and -clip you can generate collages.", },
  142.   { "name",       NAME,       NULL, "\
  143. Specify that the next argument is to be the name of an image.  This is\n\
  144. useful for loading images whose names look to be options.", },
  145.   { "newoptions", NEWOPTIONS, NULL, "\
  146. Clear the options which propagate to all following images.  This is useful\n\
  147. for turning off image processing options which were specified for previous\n\
  148. images.", },
  149.   { "normalize",  NORMALIZE,  NULL, "\
  150. Normalize the image.  This expands color coverage to fit the colormap as\n\
  151. closely as possible.  It may have good effects on an image which is too\n\
  152. bright or too dark.", },
  153.   { "rotate",     ROTATE,     "degrees", "\
  154. Rotate the image by 90, 180, or 270 degrees.", },
  155.   { "smooth",     SMOOTH,     NULL, "\
  156. Perform a smoothing convolution on the image.  This is useful for making\n\
  157. a zoomed image look less blocky.  Multiple -smooth arguments will run\n\
  158. the smoother multiple times.  This option can be quite slow on large images.", },
  159.   { "xzoom",      XZOOM,      "percentage", "\
  160. Zoom the image along the X axis by a percentage.  See -zoom.", },
  161.   { "yzoom",      YZOOM,      "percentage", "\
  162. Zoom the image along the X axis by a percentage.  See -zoom.", },
  163.   { "zoom",       ZOOM,       NULL, "\
  164. Zoom the image along both axes. Values smaller than 100 will reduce the\n\
  165. size of the image, values greater than 100 will enlarge it.  See also\n\
  166. -xzoom and -yzoom.", },
  167.   { NULL,         0,          NULL, NULL }
  168. };
  169.  
  170. OptionId optionNumber(arg)
  171.      char *arg;
  172. { int a, b;
  173.  
  174.   if ((*arg) != '-')
  175.     return(OPT_NOTOPT);
  176.   for (a= 0; Options[a].name; a++) {
  177.     if (!strncmp(arg + 1, Options[a].name, strlen(arg) - 1)) {
  178.       for (b= a + 1; Options[b].name; b++)
  179.     if (!strncmp(arg + 1, Options[b].name, strlen(arg) - 1))
  180.       return(OPT_SHORTOPT);
  181.       return(Options[a].option_id);
  182.     }
  183.   }
  184.   return(OPT_BADOPT);
  185. }
  186.  
  187. static void listOptions()
  188. { int a, width;
  189.  
  190.   printf("\nThe options are:\n\n");
  191.  
  192.   width= 0;
  193.   for (a= 0; Options[a].name; a++) {
  194.     width += strlen(Options[a].name) + 2;
  195.     if (width > 78) {
  196.       printf("\n");
  197.       width= strlen(Options[a].name) + 2;
  198.     }
  199.     printf("%s%s", Options[a].name, (Options[a + 1].name ? ", " : "\n\n"));
  200.   }
  201. }
  202.  
  203. static int helpOnOption(option)
  204.      char *option;
  205. { int a, foundone;
  206.  
  207.   if (*option == '-')
  208.     option++;
  209.   foundone= 0;
  210.   for (a= 0; Options[a].name; a++)
  211.     if (!strncmp(Options[a].name, option, strlen(option))) {
  212.       printf("Option: %s\nUsage: xloadimage -%s %s\nDescription:\n%s\n\n",
  213.          Options[a].name, Options[a].name,
  214.          (Options[a].args ? Options[a].args : ""),
  215.          Options[a].description);
  216.       foundone= 1;
  217.     }
  218.   if (!foundone) {
  219.     printf("No option `%s'.\n", option);
  220.     listOptions();
  221.   }
  222.   return(foundone);
  223. }
  224.  
  225. static void literalMindedUser(s)
  226.      char *s;
  227. {
  228.   printf("The quotes around %s are unnecessary.  You don't have to be so\n\
  229. literal-minded!\n", s);
  230. }
  231.  
  232. void help(option)
  233.      char *option;
  234. { char buf[BUFSIZ];
  235.  
  236.   /* batch help facility
  237.    */
  238.  
  239.   if (option) {
  240.     if (!helpOnOption(option))
  241.       printf("\
  242. Type `xloadimage -help [option ...]' to get help on a particular option or\n\
  243. `xloadimage -help' to enter the interactive help facility.\n\n");
  244.     return;
  245.   }
  246.  
  247.   /* interactive help facility
  248.    */
  249.  
  250.   printf("\nXloadimage Interactive Help Facility\n\n");
  251.   printf("\
  252. Type `?' for a list of options, or `.' or `quit' to leave the interactive\n\
  253. help facility.\n");
  254.   for (;;) {
  255.     printf("help> ");
  256.     buf[BUFSIZ - 1]= '\0';
  257.     fgets(buf, BUFSIZ - 1, stdin);
  258.     while(buf[strlen(buf) - 1] == '\n')
  259.       buf[strlen(buf) - 1]= '\0';
  260.  
  261.     /* help keywords
  262.      */
  263.  
  264.     if (!strcmp(buf, "")) {
  265.       printf("Type `?' for a list of options\n");
  266.       continue;
  267.     }
  268.     if (!strcmp(buf, "?"))
  269.       ;
  270.     else if (!strcmp(buf, "quit") || !strcmp(buf, "."))
  271.       exit(0);
  272.     else if (!strcmp(buf, "`?'"))
  273.       literalMindedUser("the question mark");
  274.     else if (!strcmp(buf, "`quit'")) {
  275.       literalMindedUser("quit");
  276.       exit(0);
  277.     }
  278.     else if (!strcmp(buf, "`.'")) {
  279.       literalMindedUser("the period");
  280.       exit(0);
  281.     }
  282.     else if (helpOnOption(buf))
  283.       continue;
  284.     listOptions();
  285.     printf("\
  286. You may get this list again by typing `?' at the `help>' prompt, or leave\n\
  287. the interactive help facility with `.' or `quit'.\n");
  288.   }
  289. }
  290.  
  291.